package com.xb.sourest;

import android.app.Application;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;

import androidx.annotation.NonNull;
import androidx.multidex.MultiDex;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.DefaultRefreshFooterCreator;
import com.scwang.smartrefresh.layout.api.DefaultRefreshHeaderCreator;
import com.scwang.smartrefresh.layout.api.RefreshFooter;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.xb.sourest.api.ApiClent;
import com.xb.sourest.base.BuildParams;
import com.xb.sourest.im.HxSdkHelper;
import com.xb.sourest.service.MainService;
import com.xd.navi.AppUtils;
import com.xd.navi.log.KLog;

import java.lang.reflect.Field;

public class AndroidApplication extends Application {

    public static Context application;
    private MainService mMainService;


    @Override
    public void onCreate() {
        super.onCreate();
        application = this;
        //初始化网络请求框架
        ApiClent.getInstance();
        MultiDex.install(this);
        initSmartRefresh();
        getGradlePropertiesParams();
        //初始化Klog
        KLog.init(BuildParams.IS_DEBUG);
        //初始化环信sdk
        HxSdkHelper.getInstance().initSdkOptions(this, BuildParams.IS_DEBUG);
        //启动Service，绑定环信监听
        bindService(new Intent(AndroidApplication.this, MainService.class), mServiceConnection, Context.BIND_AUTO_CREATE);
    }

    /**
     * 获取gradle.properties里映射到BuildConfig的参数
     */
    private void getGradlePropertiesParams() {
        try {
            String packageName = AppUtils.getMyPackageName(this);
            //根据BuildConfig路径获取实例对象
            Class buildConfig = Class.forName(packageName + ".BuildConfig");
            BuildParams.IS_DEBUG = getConfigField(buildConfig, "IS_DEBUG");
            BuildParams.EASEMOB_APPKEY = getConfigField(buildConfig, "EASEMOB_APPKEY");
            BuildParams.MIPUSH_APPID = getConfigField(buildConfig, "MIPUSH_APPID");
            BuildParams.MIPUSH_APPKEY = getConfigField(buildConfig, "MIPUSH_APPKEY");
            BuildParams.HWPUSH_APPID = getConfigField(buildConfig, "HWPUSH_APPID");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
    }

    private <T> T getConfigField(Class buildConfig, String field) throws NoSuchFieldException, IllegalAccessException {
        Field field1 = buildConfig.getField(field);
        boolean access = field1.isAccessible();
        if (!access)
            field1.setAccessible(true);
        T value = (T) field1.get(buildConfig);
        if (!access)
            field1.setAccessible(false);
        return value;
    }

    /**
     * 初始化smart的头和尾
     */
    private void initSmartRefresh() {
        SmartRefreshLayout.setDefaultRefreshHeaderCreator(new DefaultRefreshHeaderCreator() {
            @NonNull
            @Override
            public RefreshHeader createRefreshHeader(@NonNull Context context, @NonNull RefreshLayout layout) {
                return new ClassicsHeader(context);
            }
        });

        SmartRefreshLayout.setDefaultRefreshFooterCreator(new DefaultRefreshFooterCreator() {
            @NonNull
            @Override
            public RefreshFooter createRefreshFooter(@NonNull Context context, @NonNull RefreshLayout layout) {
                return new ClassicsFooter(context);
            }
        });

    }


    @Override
    public void onTerminate() {
        unbindService(mServiceConnection);
        super.onTerminate();
    }

    private ServiceConnection mServiceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mMainService = ((MainService.MainServiceBinder) service).getService();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            mMainService = null;
        }
    };

    public static Context getApplication() {
        return application;
    }
}
